<template>
	<div class="pages">
		<div class="search">
			<van-search background="#4674C9" @click-right-icon="searchLists" v-model="search" left-icon="" right-icon="search"
				placeholder="请输入搜索关键词" />
		</div>
		<div class="container">
			<!-- 发送平台 star -->
			<div class="lists-module-tips">
				<div class="lists">
					<van-pull-refresh class="pull-refresh" v-model="lists.refresh.loading" @refresh="onRefresh">
						<van-list v-model:loading="lists.more.loading" :finished="lists.more.finished" finished-text="没有更多了"
							@load="onLoad">
							<van-checkbox-group v-model="checked">
								<div class="item" v-for="(item, idx) in lists.lists" :key="idx">
									<div class="info">
										<van-checkbox shape="square" icon-size="12px" :name="idx"></van-checkbox>
										<div class="text">
											<div class="content" v-text="item.targetOrgName"></div>
											<div class="label text-overflow">
												<span class="company" v-text="'接收人：' + item.recipientName"></span>
												<span @click="detail(item)" class="detail">详情</span>
											</div>
										</div>
									</div>
									<van-divider />
								</div>
							</van-checkbox-group>
						</van-list>
					</van-pull-refresh>
				</div>
			</div>
			<van-popup v-model:show="lists.detail.show" position="bottom" close-on-popstate :style="{ height: '30%' }">
				<div class="popup-container">
					<div class="tools">
						<div class="cancel" @click="lists.detail.show = false">取消</div>
						<div class="del">删除信息</div>
					</div>
					<van-divider class="line" />
					<div class="content">
						<div class="text">
							<div class="title" v-text="'平台名称：'"></div>
							<div class="value" v-text="lists.detail.info.targetOrgName"></div>
						</div>
						<div class="text">
							<div class="title" v-text="'属地单位：'"></div>
							<div class="value" v-text="lists.detail.info.company"></div>
						</div>
						<div class="text">
							<div class="title" v-text="'接收人：'"></div>
							<div class="value" v-text="lists.detail.info.recipientName"></div>
						</div>
						<div class="text">
							<div class="title" v-text="'联系方式：'"></div>
							<div class="value" v-text="lists.detail.info.recipientUserPhone"></div>
						</div>
					</div>
				</div>
			</van-popup>
			<!-- 发送平台 end -->
		</div>
		<div class="menu">
			<div class="btn blue btn-w-2" @click="addPlatfrom">新增平台</div>
			<div class="btn red btn-w-2" @click="delPlatfrom">删除平台</div>
		</div>
	</div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Page } from '@/api/types/public'
import { taskInfo, taskUpdate, tasktargetinfo } from '@/api/todo'
import { deepClone, dateFormat } from '@/utils/utils'

const route = useRoute();
const router = useRouter();

// 搜索
const search = ref('');
const searchLists = () => {
	// console.log('search', search.value);
}

const lists = reactive({
	refresh: {
		loading: false
	},
	more: {
		loading: false,
		finished: false
	},
	pages: {
		page: 1,
		size: 10
	} as Page,
	lists: [] as any,
	detail: {
		show: false,
		info: {} as any
	}
})

// 选中列表
const checked = ref([]);

// 下拉刷新回调
const onRefresh = () => {
	// 清空列表数据
	lists.more.finished = false
	lists.pages.page = 1
	// 重新加载数据
	// 将 loading 设置为 true，表示处于加载状态
	lists.more.loading = true
	onLoad();
}

// 加载更多回调
const onLoad = async () => {
	const params = {
		taskId: route.query.taskId as string,
		page: lists.pages.page,
		size: lists.pages.size,
	}
	try {
		const res = await tasktargetinfo(params)

		let temp = res.data.records
		if (lists.refresh.loading) {
			lists.lists = [];
			lists.refresh.loading = false;
		}
		lists.lists.push(...temp);
		lists.more.loading = false;

		lists.pages.page++

		if (!temp.length) {
			lists.more.finished = true;
		} else {
			lists.more.finished = false;
		}
	} catch (error: any) {
		lists.refresh.loading = false;
		lists.more.loading = false;
		lists.more.finished = true;
	}
}

// 发送平台详情
const detail = (info: any) => {
	lists.detail.info = info;
	lists.detail.show = true;
}

// 新增平台
const addPlatfrom = () => {
	toPage(paths.add);
}

// 删除平台
const delPlatfrom = () => {
	console.log(checked.value);
}

const paths = reactive({
	send: '/todo/taskapprovalsend',
	sendback: '/todo/taskapprovalsendback',
	process: '/todo/taskapprovalprocess',
	add: '/todo/taskplatfromadd',
})

const toPage = (path: any, params: any = {}) => {
	router.push({ path, query: params })
}
onMounted(() => {
})
</script>
<style lang="scss" scoped>
.pages {
	height: 100%;
	display: flex;
	flex-direction: column;

	.container {
		flex: 1;
		height: 0;
		box-sizing: border-box;
		padding: 12px;
		display: flex;
		flex-direction: column;

		// 列表模板-提示
		.lists-module-tips {
			background: #ffffff;
			border-radius: 5px;
			box-shadow: 0.5px 0.87px 4.5px 1px rgba(0, 0, 0, 0.05);
			box-sizing: border-box;
			flex: 1;
			height: 0;
			overflow-y: scroll;

			.lists {
				padding-top: 16px;

				.item {
					display: flex;
					flex-direction: column;
					padding: 0 12px 0 12px;

					.info {
						display: flex;
						flex-direction: row;

						.circle {
							width: 4px;
							height: 4px;
							background-color: $primary-color;
							border-radius: 50%;
							margin-top: 6px;
							margin-right: 8px;
						}

						&:deep(.van-checkbox) {
							align-items: flex-start;
							margin-top: 2px;
							margin-right: 8px;

							.van-checkbox__icon--checked {
								.van-icon {
									background-color: $primary-color;
									border-color: $primary-color;
								}
							}

							.van-icon-success:before {
								position: absolute;
								left: 50%;
								height: 50%;
								transform: translate(-50%, -50%);
							}
						}

						.text {
							flex: 1;

							.content {
								font-size: 14px;
								font-weight: bold;
								text-align: left;
								color: #181818;
								margin-bottom: 8px;
							}

							.label {
								font-size: 10px;
								font-weight: bold;
								color: #646464;
								white-space: nowrap;
								display: flex;
								align-items: center;
								justify-content: space-between;

								.company {
									margin-right: 4px;
								}

								.detail {
									color: $primary-color;
									margin-left: 4px;
								}
							}
						}
					}
				}
			}
		}
	}

	.popup-container {
		.tools {
			display: flex;
			align-items: center;
			justify-content: space-between;
			box-sizing: border-box;
			padding: 9px 12px;

			.cancel {
				font-size: 14px;
				font-weight: bold;
				color: #646464;
			}

			.del {
				font-size: 14px;
				font-weight: bold;
				color: #ef0d0d;
			}
		}

		.line {
			margin-top: 0;
		}

		.content {
			box-sizing: border-box;
			padding: 16px 24px;

			.text {
				display: flex;
				align-items: center;
				margin-bottom: 16px;

				&:last-child {
					margin-bottom: 0;
				}

				.title {
					font-size: 14px;
					font-weight: bold;
					color: #181818;
				}

				.value {
					font-size: 14px;
					font-weight: bold;
					color: #646464;
				}
			}
		}
	}

	.menu {
		display: flex;
		align-items: center;
		justify-content: space-evenly;
		box-sizing: border-box;
		padding: 10px 0;
		background: #ffffff;
		box-shadow: 0px -1px 2.5px 1px rgba(0, 0, 0, 0.03), -0.5px 0px 0px 0.5px #ebebeb inset;

		.btn {
			box-sizing: border-box;
			padding: 8px 0;
			border-radius: 5px;
			font-size: 14px;
			font-weight: bold;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.btn-w-3 {
			width: 30%;
		}

		.btn-w-2 {
			width: 45%;
		}

		.blue {
			color: $primary-color;
			background-color: #F1F4FF;
		}

		.red {
			color: #EF0D0D;
			background-color: #FFF1F2;
		}
	}
}
</style>